 <template>
  <div id="pro">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home/personalcenter/todo' }"
        >个人中心</el-breadcrumb-item
      >
      <el-breadcrumb-item>我的资料</el-breadcrumb-item>
    </el-breadcrumb>
    <el-descriptions class="margin-top" title="基本信息" :column="3" border>
      <template slot="extra">
        <el-button type="primary" size="medium" @click="onchange"
          >修改</el-button
        >
      </template>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-user"></i>
          姓名
        </template>
        {{ tab.name }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          工号
        </template>
        {{ tab.work_id }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-office-building"></i>
          头像
        </template>
        <template>
          <div class="demo-fit">
            <div class="block" v-for="fit in fits" :key="fit">
              <el-avatar
                shape="square"
                :size="100"
                :fit="fit"
                :src="url"
              ></el-avatar>
            </div>
          </div>
        </template>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          员工类型
        </template>
        {{ tab.type }}
        <template>
          <img id="myimg" alt="" />
        </template>
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          入职时间
        </template>
        {{ tab.time }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          工龄
        </template>
        1年
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          所属部门
        </template>
        {{ tab.department_name }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          直接上级
        </template>
        {{ tab.superior }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          岗位
        </template>
        {{ tab.post_name }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions class="margin-top" title="个人信息" :column="3" border>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          性别
        </template>
        {{ tab.sex }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-mobile-phone"></i>
          手机号
        </template>
        {{ tab.phone }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          身份证号
        </template>
        {{ tab.idcard }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-location-outline"></i>
          邮箱
        </template>
        {{ tab.email }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label">
          <i class="el-icon-office-building"></i>
          联系地址
        </template>
        {{ tab.address }}
      </el-descriptions-item>
    </el-descriptions>
    <!-- 修改 -->
    <el-dialog title="修改资料" :visible.sync="ischange">
      <el-form :model="form">
        <el-form-item label="工号" :label-width="formLabelWidth" prop="work_id">
          <el-input
            v-model="form.work_id"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="figure" :label-width="formLabelWidth">
          <el-form-item label="图标">
            <el-upload
              class="avatar-uploader"
              action="http"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form-item>
        <el-form-item
          label="员工类型"
          prop="type"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="入职时间"
          prop="post_time"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.post_time" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位" prop="jobs" :label-width="formLabelWidth">
          <el-input v-model="form.jobs" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex" :label-width="formLabelWidth">
          <el-input v-model="form.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item
          label="身份证号"
          prop="idcard"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.idcard" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ischange = false">取 消</el-button>
        <el-button type="primary" @click="onchangeok">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="less">
#pro {
  .el-descriptions__header {
    margin-bottom: 10px;
    margin-top: 20px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>
export default {
  data() {
    return {
      fits: ["fill"],
      url: "",
      ischange: false,
      form: {},
      tab: {},
      formLabelWidth: "120px",
      imageUrl: "",
    };
  },
  methods: {
    //  上传头像
    handleAvatarSuccess(res, file) {
      console.log(file);
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl);
      // this.form.figure = this.imageUrl;
      this.form.figure = res.url;
      this.url = this.imageUrl;
      console.log(res.path);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onchange() {
      this.ischange = true;
      this.form = this.tab;
    },
    // 确定修改按钮
    onchangeok() {
      this.ischange = false;
      console.log(this.form);
      this.$axios({
        url: "/meetmanagement/admin/edi",
        method: "post",
        data: this.form,
      }).then((res) => {
        console.log(res);
        this.getuser();
        if (res.data.code == 200) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
        }
      });
    },
    getuser() {
      this.$axios({
        url: "/meetmanagement/admin/get",
        data: {
          userid: String(JSON.parse(sessionStorage.getItem("user")).work_id),
        },
        method: "post",
      }).then((res) => {
        console.log(res.data.data);
        this.tab = res.data.data[0];
        console.log(this.tab);
        if (res.data.data[0].figure.length <= 50) {
          this.url = require("../assets/images/" +
            res.data.data[0].figure +
            ".jpg");
        } else {
          // this.$axios({
          //   url: "/meetmanagement/admin/api/upload/",
          //   method: "post",
          // }).then((res) => {
          //   console.log(res);
          // });
          // this.url = res.data.data[0].figure;
          // this.url = URL.createObjectURL(`${res.data.data[0].figure}`);
          // getimg();
        }
      });
    },
    // getimg() {
    //   var xhr = new XMLHttpRequest();
    //   xhr.open(
    //     "post",
    //     "http://192.168.1.44:8787/meetmanagement/admin/api/upload",
    //     true
    //   );
    //   xhr.responseType = "blob";
    //   xhr.onload = function () {
    //     if (this.status == 200) {
    //       var blob = this.response;
    //       document.getElementById("myimg").src = URL.createObjectURL(blob);
    //     }
    //   };
    //   xhr.send();
    // },
  },

  created() {
    // this.getimg();
    this.getuser();
  },
};
</script>